package com.xy.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Configuration
public class DelayQueueConfig {

    private static final String DELAY_QUEUE = "delayQueue";

    private static final String TARGET_QUEUE = "targetQueue";

    private static final String DLX_EXCHANGE = "dlxExchange";

    @Bean
    public Queue delayQueue() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange", DLX_EXCHANGE);
        args.put("x-dead-letter-routing-key", TARGET_QUEUE);
        return new Queue(DELAY_QUEUE, true, false, false, args);
    }

    @Bean
    public Queue targetQueue() {
        return new Queue(TARGET_QUEUE);
    }

    @Bean
    public DirectExchange dlxExchange() {
        return new DirectExchange(DLX_EXCHANGE);
    }

    @Bean
    public Binding bindingTargetQueue(Queue targetQueue, DirectExchange dlxExchange) {
        return BindingBuilder.bind(targetQueue).to(dlxExchange).with(TARGET_QUEUE);
    }
}
